package com.dule.Medic.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.corelib.components.Zone;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.dule.Medic.beans.MedEntryBean;
import com.dule.Medic.model.Doctor;
import com.dule.Medic.model.MedEntry;
import com.dule.Medic.model.Patient;
import com.dule.Medic.service.MedEntryService;
import com.dule.Medic.service.PatientService;

public class Search 
{
	@SuppressWarnings("unused")
	@Property      
	@SessionState      
	private Doctor doctor;    
	private boolean doctorExists;  
	
	@Inject       
	private MedEntryService medEntryService;    
	@Inject       
	private PatientService patientService; 
	
	@SuppressWarnings("unused")
	@Property
	private MedEntry medentry;
	@SuppressWarnings("unused")
	@Property
	private Patient patient;
	
	@Property
	private List<MedEntry> medEnteries;
	@Property
	private List<Patient> patients;
	
	@Property
	private String name1;
	@Property
	private String name2;
	
	@InjectComponent
	private Zone MedentriesZone;
	@InjectComponent
	private Zone PatientsZone;
	
	@SetupRender   
	public void setUp()
	{    
		medentry = new MedEntryBean();     
	}
	
	
	public Object onActivate()
	{      
		if(doctorExists)
		{                
			return null;      
		}          
		return FirstPage.class;   
	}      
	
	 @OnEvent(value = "submit",component="SearchME") 
	 Object onSearchMe() 
	 {
		try
		{
			List<MedEntry> list =medEntryService.retrieveAll();
			medEnteries=new ArrayList<MedEntry>(0);
			for(MedEntry me:list)
	        {
				if(me.getPatient().getFirstName().toLowerCase().contains(name1.toLowerCase()) || 
						me.getPatient().getLastName().toLowerCase().contains(name1.toLowerCase()) ||
						(me.getPatient().getFirstName().toLowerCase()+me.getPatient().getLastName().toLowerCase()).contains(name1.toLowerCase()) ||
						(me.getPatient().getLastName().toLowerCase()+me.getPatient().getFirstName().toLowerCase()).contains(name1.toLowerCase()))
				{
					medEnteries.add(me);
				}
	        }
		}
		catch (Exception e) 
		{
			// TODO: handle exception
		}
		return MedentriesZone.getBody();
	}
	 
	 @OnEvent(value = "submit",component="SearchPT") 
	 Object onSearchPt() 
	 {
		 try
		 {
			List<Patient> list =patientService.retrieveAll();
			patients=new ArrayList<Patient>(0);
			for(Patient pt:list)
	        {
				if(pt.getFirstName().toLowerCase().contains(name2.toLowerCase()) || 
						pt.getLastName().toLowerCase().contains(name2.toLowerCase()) ||
						(pt.getFirstName().toLowerCase()+pt.getLastName().toLowerCase()).contains(name2.toLowerCase()) ||
						(pt.getLastName().toLowerCase()+pt.getFirstName().toLowerCase()).contains(name2.toLowerCase()))
				{
					patients.add(pt);
				}
	        }
		}
		catch (Exception e) 
		{
				// TODO: handle exception
		}
		return PatientsZone.getBody();
	}

}
